package com.abcdefg.mybatisdemo.mapper;

import org.apache.ibatis.annotations.*;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    @Select("select * from user_info")
    List<UserInfo> selectAll();

    @Select("select * from user_info where id=#{userId}")
    UserInfo selectById(@Param("userId") Integer id);
    @Select("select * from user_info where username=#{param1} " +
            "and password =#{param2}")
    UserInfo selectByUsernameAndPassword(String username, String password);

    @Select("select * from user_info where username=#{param2} " +
            "and password =#{param1}")
    UserInfo selectByUsernameAndPassword2(String username, String password);

    /**
     *
     * @param userInfo
     * @return
     */
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into user_info(username,password,age) VALUES(#{username},#{password},#{age})")
    Integer insert(UserInfo userInfo);
    @Insert("insert into user_info(username,password,age) VALUES(#{userInfo.username},#{userInfo.password},#{userInfo.age})")
    Integer insert2(@Param("userInfo") UserInfo userInfo);

    @Delete("delete  from user_info where id=#{id} ")
    Integer deleteById(Integer id);

    @Update("update user_info set username=#{username}  where id=#{id}")
    Integer updateById(UserInfo userInfo);


    /**
     * 第一种解决select查询方式
     * @param age
     * @return
     */
    @Select("select id as id,username as username,password as password,age as age,gender as gender," +
            "phone as phone ,delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from user_info where age=#{age}")
    List<UserInfo> selectByAge(Integer age);


    @Select("select id ,username ,password ,age,gender ," +
            "phone  ,delete_flag ,create_time ,update_time from user_info " +
            "where delete_flag=#{deleteFlag}")
    @Results(id = "resultMap",value={
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime")
    })
    List<UserInfo> selectByDeleteFlag(Integer deleteFlag);



    @ResultMap("resultMap")
    @Select("select * from user_info where username=#{username}")
    UserInfo selectByUsername(String username);
}
